﻿using MaterialDesignThemes.Wpf;
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp1.Report;

namespace WpfApp1
{
    /// <summary>
    /// DeviceAddWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceAddWindow : Window
    {
        public DeviceAddWindow()
        {
            InitializeComponent();
        }

        public string id { get; set; }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            setlang();
            getPrinters();
            getConfigs();
        }

        private void setlang()
        {
            if (lang.Equals("ug"))
            {
                this.FlowDirection = FlowDirection.RightToLeft;
                this.Title = "پىرىنتېر  قوشۇش";
                HintAssist.SetHint(combo0, "پىرىنتېر ئىسمى");
                HintAssist.SetHint(combo1, "بېسىش تۈرى");

                HintAssist.SetHint(txt_name, "ئىسمى");
                HintAssist.SetHint(txt_print_count, "بېسىش سانى");
                HintAssist.SetHint(txt_print_paper_width, "قەغەز كەڭلىكى");
                HintAssist.SetHint(txt_print_paper_height, "قەغەز ئېگىزلىكى");

                HintAssist.SetHint(combo2, "ھالىتى");

                btn_cancel.Content = "قالدۇرۇش";
                btn_ok.Content = "ساقلاش";
                btn_test.Content = "سىناش";

            }
            else
            {
                this.FlowDirection = FlowDirection.LeftToRight;
                this.Title = "设备添加";
                HintAssist.SetHint(combo0, "设备名称");
                HintAssist.SetHint(combo1, "打印类型");

                HintAssist.SetHint(txt_name, "别名");
                HintAssist.SetHint(txt_print_count, "打印数量");
                HintAssist.SetHint(txt_print_paper_width, "纸张宽度");
                HintAssist.SetHint(txt_print_paper_height, "纸张长度");


                HintAssist.SetHint(combo2, "状态");

                btn_cancel.Content = "取消";
                btn_ok.Content = "确定";
                btn_test.Content = "测试打印";
            }
        }

        private void getConfigs()
        {
            combo1.Items.Clear();

            //print type
            DataTable dt3 = sqlite.getData("select * from other_config  where type='printer_type' and lang='" + lang + "'");

            for (int i = 0; i < dt3.Rows.Count; i++)
            {
                string idd = dt3.Rows[i]["id"].ToString();
                string value = dt3.Rows[i]["value"].ToString();
                ComboBoxItem item = new ComboBoxItem();
                item.Content = value;
                item.Tag = idd;
                combo1.Items.Add(item);

            }

            //print type
            DataTable dt4 = sqlite.getData("select * from other_config  where type='printer_status'  and lang='" + lang + "'");

            for (int i = 0; i < dt4.Rows.Count; i++)
            {
                string idd = dt4.Rows[i]["id"].ToString();
                string value = dt4.Rows[i]["value"].ToString();
                ComboBoxItem item = new ComboBoxItem();
                item.Content = value;
                item.Tag = idd;
                combo2.Items.Add(item);

            }

            if (!string.IsNullOrEmpty(id))
            {
                DataRow row = sqlite.getDataRow("select * from printer where id='" + id + "'");
                if (row != null)
                {
                    string name = row["name"].ToString();
                    txt_name.Text = name;

                    string device_name = row["device_name"].ToString();

                    for (int i = 0; i < combo0.Items.Count; i++)
                    {
                        ComboBoxItem item0 = (ComboBoxItem)combo0.Items[i];
                        string tag = item0.Content.ToString();
                        if (tag.Equals(device_name))
                        {
                            combo0.SelectedItem = item0;
                        }
                    }

                    string print_type = row["print_type"].ToString();
                    for (int i = 0; i < combo1.Items.Count; i++)
                    {
                        ComboBoxItem item0 = (ComboBoxItem)combo1.Items[i];
                        string tag = item0.Tag.ToString();
                        if (tag.Equals(print_type))
                        {
                            combo1.SelectedItem = item0;
                        }
                    }

                    string status = row["status"].ToString();
                    for (int i = 0; i < combo2.Items.Count; i++)
                    {
                        ComboBoxItem item0 = (ComboBoxItem)combo2.Items[i];
                        string tag = item0.Tag.ToString();
                        if (tag.Equals(status))
                        {
                            combo2.SelectedItem = item0;
                        }
                    }

                    string print_ct = row["print_count"].ToString();
                    txt_print_count.Text = print_ct;


                    string paper_width = row["paper_width"].ToString();
                    txt_print_paper_width.Text = paper_width;


                    string paper_height = row["paper_height"].ToString();
                    txt_print_paper_height.Text = paper_height;

                    string isAuto = row["automatic"].ToString();
                    if (isAuto.Equals("1"))
                    {
                        isAutomatc1.IsChecked = true;
                    }
                    else
                    {
                        isAutomatc2.IsChecked = true;
                    }


                }
            }
        }

        private void getPrinters()
        {

            List<string> list = MyPrinter.GetLocalPrinters();

            //System.Collections.ArrayList list = Printer.GetPrinterList();

            for (int i = 0; i < list.Count; i++)
            {
                string name = list[i].ToString();
                ComboBoxItem item = new ComboBoxItem();
                item.Content = name;
                combo0.Items.Add(item);
            }
        }

        SQLiteHelper sqlite = new SQLiteHelper();


        public string lang { get; set; }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string name = btn.Name;
            switch (name)
            {
                case "btn_cancel":
                    this.Close();
                    break;
                case "btn_ok":

                    string printer_alias = txt_name.Text;
                    string print_ct = txt_print_count.Text;

                    string paper_width = txt_print_paper_width.Text;
                    string paper_height = txt_print_paper_height.Text;

                    if (string.IsNullOrEmpty(printer_alias) || string.IsNullOrEmpty(print_ct))
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = "پىرىنېتېر ئىسمى ياكى بېسىش سانى قۇرۇق قالدى";
                        }
                        else
                        {
                            mg = "名称或打印数量为空!";
                        }
                        MessageBox.Show(mg);
                        return;
                    }

                    if (string.IsNullOrEmpty(paper_width) || string.IsNullOrEmpty(paper_height))
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = "قەغەز چوڭلۇقى قۇرۇق بولسا بولمايدۇ";
                        }
                        else
                        {
                            mg = "纸张大小不能为空";
                        }
                        MessageBox.Show(mg);
                        return;
                    }
                    int width = 0;
                    int.TryParse(paper_width, out width);
                    if (width <= 0)
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = " قەغەز كەڭلىكى 0 دىن چوڭ بولۇشى كېرەك ";
                        }
                        else
                        {
                            mg = "纸张宽度必须大于0";
                        }
                        MessageBox.Show(mg);
                        return;

                    }

                    int height = 0;
                    int.TryParse(paper_height, out height);
                    if (height <= 0)
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = " قەغەز ئېگىزلىكى 0 دىن چوڭ بولۇشى كېرەك ";
                        }
                        else
                        {
                            mg = "纸张高度必须大于0";
                        }
                        MessageBox.Show(mg);
                        return;
                    }

                    if (combo0.SelectedItem == null || combo1.SelectedItem == null || combo2.SelectedItem == null)
                    {
                        return;
                    }

                    ComboBoxItem item0 = (ComboBoxItem)combo0.SelectedItem;
                    string device_name = item0.Content.ToString();

                    ComboBoxItem item1 = (ComboBoxItem)combo1.SelectedItem;
                    string print_type = item1.Tag.ToString();

                    ComboBoxItem item2 = (ComboBoxItem)combo2.SelectedItem;
                    string status = item2.Tag.ToString();

                    int isAuto = 0;

                    if (isAutomatc1.IsChecked == true)
                    {
                        isAuto = 1;
                    }


                    int pr_ct = 1;
                    int.TryParse(print_ct, out pr_ct);

                    if (pr_ct < 1 || pr_ct > 5)
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = " بېسىش سانى 1 دىن 5 كىچە بولسا نورمال";
                        }
                        else
                        {
                            mg = "打印数量范围1--5";
                        }
                        MessageBox.Show(mg);
                        return;
                    }

                    if (string.IsNullOrEmpty(id))
                    {
                        add(printer_alias, device_name, print_type, status, pr_ct, width, height,isAuto);
                    }
                    else
                    {
                        update(id, printer_alias, device_name, print_type, status, pr_ct, width, height,isAuto);
                    }
                    string msg = "";
                    if (lang.Equals("ug"))
                    {
                        msg = "ئوڭۇشلۇق بولدى";
                    }
                    else
                    {
                        msg = "成功";
                    }

                    MessageBox.Show(msg);

                    this.Close();

                    break;
                case "btn_test":

                    item0 = (ComboBoxItem)combo0.SelectedItem;
                    device_name = item0.Content.ToString();

                    item1 = (ComboBoxItem)combo1.SelectedItem;
                    print_type = item1.Tag.ToString();

                    printer_alias = txt_name.Text;
                    print_ct = txt_print_count.Text;

                    paper_width = txt_print_paper_width.Text;
                    paper_height = txt_print_paper_height.Text;

                    if (string.IsNullOrEmpty(printer_alias) || string.IsNullOrEmpty(print_ct))
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = "پىرىنېتېر ئىسمى ياكى بېسىش سانى قۇرۇق قالدى";
                        }
                        else
                        {
                            mg = "名称或打印数量为空!";
                        }
                        MessageBox.Show(mg);
                        return;
                    }

                    if (string.IsNullOrEmpty(paper_width) || string.IsNullOrEmpty(paper_height))
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = "قەغەز چوڭلۇقى قۇرۇق بولسا بولمايدۇ";
                        }
                        else
                        {
                            mg = "纸张大小不能为空";
                        }
                        MessageBox.Show(mg);
                        return;
                    }
                    width = 0;
                    int.TryParse(paper_width, out width);
                    if (width <= 0)
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = " قەغەز كەڭلىكى 0 دىن چوڭ بولۇشى كېرەك ";
                        }
                        else
                        {
                            mg = "纸张宽度必须大于0";
                        }
                        MessageBox.Show(mg);
                        return;
                    }

                    height = 0;
                    int.TryParse(paper_height, out height);
                    if (height <= 0)
                    {
                        string mg = "";
                        if (lang.Equals("ug"))
                        {
                            mg = " قەغەزھ ئېگىزلىكى 0 دىن چوڭ بولۇشى كېرەك ";
                        }
                        else
                        {
                            mg = "纸张高度必须大于0";
                        }
                        MessageBox.Show(mg);
                        return;
                    }

                    if (combo0.SelectedItem == null || combo1.SelectedItem == null || combo2.SelectedItem == null)
                    {
                        return;
                    }

                    switch (print_type)
                    {
                        case "1":

                            break;
                        case "2":

                            break;
                        case "3":

                            break;
                        case "4":

                            break;
                    }

                    //MessageBox.Show(device_name);

                    testPrinter(print_type, device_name, print_ct, width, height);

                    break;
            }
        }

        private void testPrinter(string t1, string printerName, string printCount, int width, int height)
        {
            switch (t1)
            {
                case "1":
                    string report_wenjianming = "Report1.rdlc";

                    string order_number = "SN190326000001";
                    string table_name = "1_سىناق ئۈستەل";
                    string created_at = "2019-03-26 13:14";
                    string price = "20";
                    string description = "تاماققا لازا سالمىسۇن";

                    List<Orders2> orders = new List<Orders2>();
                    string food_name_ug = "سىناق تاماق";// order_detail.Rows[j]["food_name"].ToString();
                    string food_price = "20";// order_detail.Rows[j]["price"].ToString();
                    string food_number = "1";// order_detail.Rows[j]["number"].ToString();

                    int ct = 1;

                    Orders2 orders1 = new Orders2(food_name_ug, food_price, ct, table_name);
                    orders.Add(orders1);

                    List<ReportParameter> pr1 = new List<ReportParameter>();

                    string resturant_name = sqlite.getValue("select value from sys_config where name='resturant_name' and lang='" + lang + "' ", "value");

                    pr1.Add(new ReportParameter("resturant_name", resturant_name, true));
                    pr1.Add(new ReportParameter("table_name", table_name, true));
                    pr1.Add(new ReportParameter("order_number", order_number, true));
                    pr1.Add(new ReportParameter("time", created_at, true));
                    pr1.Add(new ReportParameter("price", price, true));
                    pr1.Add(new ReportParameter("description", description, true));


                    string path = Environment.CurrentDirectory;

                    LocalReport localReport = new LocalReport();
                    localReport.ReportPath = path + @"\Report\" + report_wenjianming;

                    RDLCPrint aa = new RDLCPrint(false,false);
                    
                    aa.Run(localReport, "DataSet1", orders, pr1, printerName, "0", printCount, width, height);

                    break;

                case "2":

                    report_wenjianming = "Report2.rdlc";

                    List<Orders> orders2_1 = new List<Orders>();

                    table_name = "1_سىناق ئۈستەل";
                    description = "تاماققا لازا سالمىسۇن";
                    order_number = "SN190326000001";

                    food_name_ug = "سىناق تاماق";
                    food_price = "20";
                    food_number = "1";
                    string tm = "2019:03:26 22:27:35";

                    ct = 0;
                    int.TryParse(food_number, out ct);

                    Orders orders2_2 = new Orders(food_name_ug, food_price, ct, table_name);

                    orders2_1.Add(orders2_2);

                    pr1 = new List<ReportParameter>();

                    pr1.Add(new ReportParameter("order_number", order_number, true));
                    pr1.Add(new ReportParameter("table_name", table_name, true));
                    pr1.Add(new ReportParameter("description", description, true));
                    pr1.Add(new ReportParameter("time", tm, true));

                    if (string.IsNullOrEmpty(printerName))
                    {
                        // MessageBox.Show("no printer found!");
                        break;
                    }

                    path = Environment.CurrentDirectory;

                    localReport = new LocalReport();

                    localReport.ReportPath = path + @"\Report\" + report_wenjianming;

                    aa = new RDLCPrint(false,false);
                    aa.isDetail1 = false;
                    aa.Run(localReport, "DataSet1", orders2_1, pr1, printerName, "0", printCount, width, height);

                    break;

                case "3":

                    report_wenjianming = "Report3.rdlc";

                    order_number = "SN190326000001";

                    string delivery_time = "14:15";
                    price = "20";
                    description = "تاماققا لازا سالمىسۇن";

                    string pay_type = "ئۈندىدار";
                    string delivery_fee = "1";
                    string receiver = "رۇستەم";
                    string address = "تەڭرىتاغ رايونى يەنئەن يولى رۈيدا سودا سارىيى 12_قەۋەت 1201_ئىشخانا";
                    string phone = "18129366666";

                    food_name_ug = "سىناق تاماق";
                    food_price = "20";
                    food_number = "1";
                    table_name = "";// order_detail.Rows[j]["table_name"].ToString();

                    ct = 0;
                    int.TryParse(food_number, out ct);

                    List<Orders2> orders3_1 = new List<Orders2>();

                    Orders2 orders3_2 = new Orders2(food_name_ug, food_price, ct, table_name);

                    orders3_1.Add(orders3_2);

                    pr1 = new List<ReportParameter>();

                    string platform_name = sqlite.getValue("select value from sys_config where name='platform_name' and lang='" + lang + "' ", "value");

                    pr1.Add(new ReportParameter("platform_name", platform_name, true));
                    pr1.Add(new ReportParameter("order_number", order_number, true));
                    pr1.Add(new ReportParameter("time", delivery_time, true));
                    pr1.Add(new ReportParameter("price", price, true));
                    pr1.Add(new ReportParameter("description", description, true));
                    pr1.Add(new ReportParameter("pay_type", pay_type, true));
                    pr1.Add(new ReportParameter("delivery_fee", delivery_fee, true));
                    pr1.Add(new ReportParameter("receiver", receiver, true));
                    pr1.Add(new ReportParameter("address", address, true));
                    pr1.Add(new ReportParameter("phone", phone, true));

                    path = Environment.CurrentDirectory;

                    localReport = new LocalReport();
                    localReport.ReportPath = path + @"\Report\" + report_wenjianming;

                    aa = new RDLCPrint(false,false);

                    aa.Run2(localReport, "DataSet1", orders3_1, pr1, printerName, "0", printCount, width, height);

                    break;

                case "4":

                    report_wenjianming = "Report4.rdlc";

                    List<Orders> orders4_1 = new List<Orders>();

                    table_name = "";
                    description = "تاماققا لازا سالمىسۇن";
                    order_number = "SN190326000001";

                    food_name_ug = "سىناق تاماق";
                    food_price = "20";
                    food_number = "1";

                    ct = 0;
                    int.TryParse(food_number, out ct);

                    Orders orders4_2 = new Orders(food_name_ug, food_price, ct, table_name);

                    orders4_1.Add(orders4_2);

                    pr1 = new List<ReportParameter>();

                    string time = "14:15";

                    pr1.Add(new ReportParameter("order_number", order_number, true));
                    pr1.Add(new ReportParameter("time", time, true));
                    pr1.Add(new ReportParameter("description", description, true));

                    path = Environment.CurrentDirectory;

                    localReport = new LocalReport();

                    localReport.ReportPath = path + @"\Report\" + report_wenjianming;


                    aa = new RDLCPrint(false,false);
                    aa.isDetail2 = false;
                    aa.Run2(localReport, "DataSet1", orders4_1, pr1, printerName, "0", printCount, width, height);

                    break;
            }
        }



        private void add(string name, string device_name, string print_type, string status, int print_count, int paper_width, int paper_height,int automatic)
        {

            string sql = "select * from printer where name='" + name + "' and device_name='" + device_name + "' and print_type='" + print_type + "' and lang='" + lang + "'";
            DataTable dt = sqlite.getData(sql);
            if (dt.Rows.Count == 0)
            {
                sql = "insert into printer (name,device_name," +
                    "print_type,status,lang,print_count,paper_width,paper_height,automatic) values ('" + name +
                    "','" + device_name + "','" + print_type + "','" + status + "','" + lang + "','" + print_count + "','" + paper_width + "','" + paper_height + "','"+automatic+"')";
                sqlite.insert(sql);
            }
        }

        private void update(string id, string name, string device_name, string print_type, string status, int print_count, int paper_width, int paper_height,int automatic)
        {

            string sql = "";
            sql = "update printer set  name='" + name + "',device_name='" + device_name + "'," +
                    "print_type='" + print_type + "',status='" + status + "',lang='" + lang + "',print_count='"
                    + print_count + "',paper_width='" + paper_width + "',paper_height='" + paper_height + "',automatic='"+ automatic + "' where id='" + id + "' ";
            sqlite.update(sql);

        }


    }
}

